home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / arith2.com / DECODE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-02-27  |  5.7 KB  |  206 lines

  1. PROGRAM decode;
  2.  
  3. {$F+}
  4.  
  5.         { ------------------------------------------------------------------
  6.  
  7.           This program and its associates implement in Turbo Pascal v5
  8.           the aritmetic encoding/decoding algorithms presented in the papers
  9.  
  10.           "Arithmetic Coding for Data Compression"
  11.  
  12.                    by Ian     H. Witten
  13.                       Radford M. Neal
  14.                       John    G. Cleary
  15.  
  16.           pp 520 - 540 of June 1987 Communications of the ACM
  17.  
  18.           and
  19.  
  20.           "An Adaptive Dependency Source Model For Data Compression"
  21.  
  22.                    by David M. Abrahamson
  23.  
  24.           pp 77 - 83 of January 1989 Communications of the ACM
  25.  
  26.           ------------------------------------------------------------------
  27.  
  28.           Implemented by Ken Westerback : CompuServe 73547,3520
  29.  
  30.           version 1.0 released 89/02/19
  31.           version 2.0 released 89/02/27
  32.  
  33.           These programs, units and associated documentation are released
  34.           into the public domain to be used and abused as your whims
  35.           dictate.
  36.  
  37.           Feel free to distribute/incorporate/improve as desired.
  38.  
  39.           >>>>> Use at your own risk! <<<<<
  40.  
  41.           Comments and suggestions welcome via CompuServe.
  42.  
  43.           ------------------------------------------------------------------
  44.         }
  45.  
  46.         USES overlay
  47.             ,dos
  48.             ,arith_de   { arithmetic decoding procedures                   }
  49.             ,fix_mod    { fixed coding model                               }
  50.             ,adap_mod   { adaptive coding model                            }
  51.             ,adp_mod    { adaptive dependency source model implementation  }
  52.             ;
  53.  
  54. {$O fix_mod }
  55. {$O adap_mod}
  56. {$O adp_mod }
  57.  
  58. var    symbol : integer;
  59.      chars_in : longint;
  60.     chars_out : longint;
  61.       decoded : file;
  62.  
  63.      char_buf : array[ 0..2047] of char; { chunks we write decoded in }
  64.  chars_in_buf : word;
  65.  
  66.   select_char : function  ( symbol : integer ) : char;
  67.  update_model : procedure ( symbol : integer );
  68.  
  69.    model_name : string;
  70.  
  71. procedure open_files;
  72.  
  73.           var     s : pathstr;
  74.               model : char;
  75.  
  76.           begin
  77.  
  78.           { first parameter is file to decode into                   }
  79.           {                                                          }
  80.           { note : will overwrite any existing file of the same name }
  81.  
  82.           if ( paramcount < 2 ) then
  83.              begin
  84.              writeln ;
  85.              writeln ( 'usage : decode <output file> <encoded file>' );
  86.              writeln ;
  87.              halt;
  88.              end;
  89.  
  90.           writeln ;
  91.           write   ( '"', paramstr ( 1 ), '" will be decoded from "'
  92.                        , paramstr ( 2 ), '"'
  93.                   );
  94.  
  95.           assign  ( decoded, paramstr ( 1 ) );
  96.           Rewrite ( decoded, 1 );
  97.  
  98.           if ( IOResult <> 0 ) then
  99.              begin
  100.              writeln ;
  101.              writeln ( 'decode can''t create output file : ', paramstr(1) );
  102.              writeln ;
  103.              end;
  104.  
  105.           model := start_decoding ( paramstr(2) );
  106.  
  107.           ovrinit ( 'decode.ovr' );
  108.  
  109.           if ovrresult <> ovrok then
  110.              begin
  111.              writeln;
  112.              writeln ( 'encode : overinit failed (', ovrresult, ')' );
  113.              writeln;
  114.              halt;
  115.              end;
  116.  
  117.           case model of
  118.              'f' : begin
  119.                    model_name   := fix_mod.model_name;
  120.                    fix_mod.start_model;
  121.                    select_char  := fix_mod.select_char;
  122.                    update_model := fix_mod.update_model;
  123.                    end;
  124.  
  125.              'a' : begin
  126.                    model_name   := adap_mod.model_name;
  127.                    adap_mod.start_model;
  128.                    select_char  := adap_mod.select_char;
  129.                    update_model := adap_mod.update_model
  130.                    end;
  131.  
  132.              'd' : begin
  133.                    model_name   := adp_mod.model_name;
  134.                    adp_mod.start_model;
  135.                    select_char  := adp_mod.select_char;
  136.                    update_model := adp_mod.update_model;
  137.                    end;
  138.  
  139.              else  begin
  140.                    writeln;
  141.                    writeln ( 'decode : invalid model "', model, '"' );
  142.                    writeln;
  143.                    halt;
  144.                    end;
  145.  
  146.              end; { model case }
  147.  
  148.           writeln ( ' using ', model_name );
  149.           writeln ;
  150.  
  151.           chars_out    := 0;
  152.           chars_in_buf := 0;
  153.  
  154.           fillchar ( char_buf, sizeof(char_buf), 0 );
  155.  
  156.           end; { open files }
  157.  
  158. procedure close_files;
  159.           begin
  160.  
  161.           inc ( chars_out, chars_in_buf );
  162.  
  163.           blockwrite ( decoded, char_buf, chars_in_buf, chars_in_buf );
  164.  
  165.           chars_in := done_decoding;
  166.  
  167.           close ( decoded );
  168.  
  169.           end; { close_files }
  170.  
  171.  
  172. BEGIN
  173.  
  174. writeln ;
  175. writeln ( 'TPascal Arithmetic Coding, by Ken Westerback, version 2.0 89/02/27' );
  176.  
  177. open_files;
  178.  
  179. while decode_symbol ( symbol ) do
  180.       begin
  181.  
  182.       char_buf[ chars_in_buf ] := select_char ( symbol );
  183.  
  184.       update_model ( symbol );
  185.  
  186.       inc ( chars_in_buf );
  187.  
  188.       if chars_in_buf = 2048 then
  189.          begin
  190.          blockwrite ( decoded, char_buf, chars_in_buf, chars_in_buf );
  191.          fillchar ( char_buf, sizeof(char_buf), 0 );
  192.          inc ( chars_out, sizeof(char_buf) );
  193.          chars_in_buf := 0;
  194.          end;
  195.  
  196.       end; { of valid symbol to decode }
  197.  
  198. close_files;
  199.  
  200. writeln ( '   characters read    : ', chars_in  );
  201. writeln ( '   characters written : ', chars_out );
  202. writeln ;
  203. writeln ( '   ', ((chars_out/chars_in)*100):5:2, ' % expansion' );
  204.  
  205. END. { arithmetic decoding }
  206.